package com.know.web;

import java.io.IOException;
import java.sql.SQLException;
import java.util.List;
import java.util.Map;

import javax.servlet.ServletException;

import javax.servlet.http.HttpSession;

import org.apache.commons.dbutils.handlers.BeanHandler;

import my.db.QueryHelper;
import my.mvc.RequestContext;

import com.know.entity.Users;
import com.kzw.bean.Record;

public class SessionLink{
	
	/**
	 * session 创建
	 * @return 
	 */
	public Record login(RequestContext rc) throws ServletException, IOException {
				Record record=new Record(true); 
		        //使用request对象的getSession()获取session，如果session不存在则创建一个
		  		HttpSession session= rc.session(true);
		        //将数据存储到session中
		  		
		        String user_name=rc.param("username");
		        System.out.println("登陆user_name:"+user_name);
		        String sql="SELECT *from `users` WHERE user_name=?";
//		        Users user=null;
//		        
//				try {
//					user = QueryHelper.me().query(sql, new BeanHandler<Users>(Users.class),username);
//				} catch (SQLException e) {
//					// TODO Auto-generated catch block
//					e.printStackTrace();
//				}
				Users user=QueryHelper.queryUnique(Users.class, sql, user_name);
		         session.setAttribute("user",user);
		         System.out.println("session的ID为："+session.getId());
		        //判断session是不是新创建的
		        if (session.isNew()) {
		        	record.setCode(1);
		            
		         }else {
		        	 record.setCode(0);		  
		        	 }
	        	return record;

	}

	

}
